<?php
class Merchant_Model_ProductStocketLog
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_ProductStocketLog();
	}
	
	public static function getInstance(){
		return new Merchant_Model_ProductStocketLog();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter(); 
	}
	/**
	*Create A Record  
	*@param rowSet $row product_stocket's data 
	*@return boolean
	*/
	public  function add($row){	 
		$row['psl_add_time'] = date("Y-m-d H:i:s");
 		return $this->_table->insert($row); 
	}
	
		
	/**
	*No Delete One Row
	*@param int $product_id 
	*@return boolean
	*/
	 public function delete($id){
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $id); 
		return $this->_table->delete($where);
	}
	
	public function getByProductId($id){	
		$select = $this->_table->select()->where('product_id = ?', $id);
		if($rowset = $this->_table->fetchRow($select)){
			return $rowset->toArray();
		}	
		return false;			
	}
	
    public function getByCondition($condition=array(),$type='*',$orderby="psl_add_time desc ",$pageSize,$page){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" product_id = ?",$condition["product_id"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	public static $type =array(
			0 => 'Basic information modify',
			1 => 'Inventory modify'
	);

	public static function getProductStocketLogByProductId($productId,$page,$pageSize){
		$results = array();
		$index=0;
		$total = 0;
		$startIndex = ($page-1)*$pageSize;
		$endIndex = $page*$pageSize;
		$serviceObj = new Merchant_Service_Product();
		$loginInfo = $serviceObj->getLoginInfo();

		$condition=array(
				'product_id'=>$productId
				);
		$wps = Merchant_Service_ProductStocketLog::getByCondition($condition,$type='*',$orderby="psl_add_time desc ",$pageSize,$page);
		
		$rowcount = Merchant_Service_ProductStocketLog::getByCondition($condition,$type='count(*)',$orderby="product_id",$pageSize,$page);
		
		
		$resultdata=array();
		if($wps){
		   foreach ($wps as $val){
		     	$log['user'] = "System";
		   	   if($val['user_id'] == -1){
		   		    $val['user'] = "Yourself";
		     	}else if($val['user_id']>0){
		     		$row=Merchant_Service_User::getByUserId($val['user_id']);
		     		$log['user'] =$row['login_id'];
		     	}
		     	
		   	    $log['type'] = self::$type[$val['psl_type']];
		   	    $val['user_id']=$log['user'];
		   	    $val['psl_type']=$log['type'];
		   	    
		   	    $resultdata[]=$val;
		   }
	        
		}
		$total=count($resultdata);
		$return["total"] = $rowcount;
		$return["results"] = $resultdata;
		return $return;
	}
	
	public function getByConditionJoin($condition,$type='*',$orderby="",$page=0,$pageSize=0){
		$select = $this->_table->getAdapter()->select();
		$table  = $this->_table->info('name');
		
		$select->from($table,$type);
		$select->join('product', "{$table}.product_id = product.product_id", '*');
		$select->where("1 =?",1);
		
		if(isset($condition["product_sku"]) && $condition["product_sku"]!=''){
			$select->where(" product_sku =? ",$condition["product_sku"]);
		}
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
			$select->where(" product.customer_id = ? ",$condition["customer_id"]);
		}
	
		if(isset($condition["product_status"]) && $condition["product_status"]!=''){
			$select->where(" product_status = ?",$condition["product_status"]);
		}
		
		if('count(*)'==$type){
			return $this->_table->getAdapter()->fetchone($select);
		}else{
			if($orderby!=''){
				$orderbyArr= explode(',',$orderby);
				foreach($orderbyArr as $order){
					$select->order($order);
				}
	
			}
			if($pageSize>0 && $page>0){
				$start = ($page-1)*$pageSize;
				$select->limit($pageSize,$start);
			}
			return $this->_table->getAdapter()->fetchAll($select);
		}
	
	}
	
}